/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */
package loginbean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class FieldMap extends java.util.HashMap
{
    private String ObjectName = null;
    private int ObjectID;
    public FieldMap(int objectid, int tenantid)
	{
		Connection conn = null;
		DBManager db = new DBManager();
		String strQuery = "";
		Statement st = null;
		ResultSet rs = null;
		try 
                {
			conn = db.getDatabaseConnection();
			strQuery = "select o.objectid, objectname, f.fieldid, f.fieldname, datatype, GUID, Value from objecttable o, fieldtable f, DataTable D "
					 + "where f.objectid=o.objectid and f.fieldid=D.FieldID and f.objectid=" + objectid + " and D.tenantid=" + tenantid;
			st = conn.createStatement();
			rs = st.executeQuery(strQuery);
			int resultsCounter = 0;  
            while(rs.next())  
            {  
             resultsCounter++;  
            }  
            if (resultsCounter ==0)  
            {  
            
            strQuery = "select o.objectid, objectname, f.fieldid, f.fieldname, datatype, 0,'Please Enter Data'  from objecttable o, fieldtable f where f.objectid=o.objectid  and f.objectid=" + objectid;
    
			st = conn.createStatement();
			rs = st.executeQuery(strQuery);
            
            } 
            else
                rs.beforeFirst();
			ObjectBean obj = new ObjectBean();
                        FieldBean field = null;
                        DataBean data = null;
                        ArrayList<DataBean> dataArray = new ArrayList<DataBean>();
			String fieldId = null, tempId = null;
			while(rs.next())
			{
				/* This list is sorted by object id's.  It has as many rows as there are fields.
				 * First we will need to create an object, then start populating fields. */
				tempId = fieldId;
				fieldId = rs.getString(3);	
                                if(ObjectName == null)
                                    ObjectName = rs.getString("objectname");
				if(!fieldId.equals(tempId))
				{
					if(field != null)
					{	
						//write to the hash map
						DataBean[] datas = new DataBean[dataArray.size()];
						for(int i=0; i<dataArray.size(); i++)
						{
							datas[i] = dataArray.get(i);
						}
						field.setDatas(datas);
						this.put(tempId, field);
					}
					field = new FieldBean();
					field.setFieldId(new Integer(fieldId).intValue());
					field.setName(rs.getString(4));
					dataArray = new ArrayList<DataBean>();
				}
	
				data = new DataBean(rs.getString(7),new Integer(rs.getString(6)).intValue());
				dataArray.add(data);
			}
			DataBean[] datas = new DataBean[dataArray.size()];
			for(int i=0; i<dataArray.size(); i++)
			{
				datas[i] = dataArray.get(i);
			}
			field.setDatas(datas);
			this.put(fieldId, field);
			conn.close();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
    public String getObjectName(){
        return this.ObjectName;
    }
    public int getObjectID(){
        return this.ObjectID;
    }
}
